package com.ray.test.mock;

import com.github.jsonzou.jmockdata.JMockData;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.util.*;

/**
 * @author bo shen
 * @Description: 类型
 * @Class: ClassDataMock
 * @Package com.ray.test.mock
 * @date 2019/12/12 14:47
 * @company <p>杭州传化陆鲸科技有限公司</p>
 * @updateRecord time(修改时间)  author(修改人)   desc(修改内容)
 */
@Slf4j
public class SetDataMock implements DataMock {
    @Override
    public Object dataMocker(Class<?> clazz, Method method, Parameter parameter) {
        try {
            Class<?> paramClazz = parameter.getType();
            Object value = new HashSet<>();
            if (parameter.getParameterizedType() instanceof ParameterizedType) {
                Class<?> pType = (Class<?>) ((ParameterizedType) parameter.getParameterizedType()).getActualTypeArguments()[0];
                if (Set.class.isAssignableFrom(paramClazz)) {
                    for (int i = 0; i < MockConfig.getListSize(method); i++) {
                        ((Set) value).add(JMockData.mock((Class<?>) pType, MockConfig.getMockConfig()));
                    }
                } else {
                    return value;
                }
            } else if (Set.class.isAssignableFrom(paramClazz)) {
                for (int i = 0; i < MockConfig.getListSize(method); i++) {
                    ((Set) value).add(JMockData.mock((Class<?>) parameter.getParameterizedType(), MockConfig.getMockConfig()));
                }
            }
            return value;
        } catch (Exception e) {
            log.info("列表模拟数据失败", e);
        }
        return null;
    }
}
